package com.rockchips.android.leanbacklauncher;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.lang.reflect.Method;
import java.util.List;

import static android.content.Context.ACTIVITY_SERVICE;
import static momo.cn.edu.fjnu.androidutils.utils.PackageUtils.getPackageName;

/**
 * Created by lqh on 2017/8/9.
 * Email:kevin.lin@rock-chips.com
 */

public class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {
    public static final String TAG = "HomeKeyEventBroadCastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "HomeKeyEventBroadCastReceiver");
        ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> a = am.getRunningTasks(Integer.MAX_VALUE);
        //PackageManager pack = context.getPackageManager();
        for (int i = 0; i < a.size(); i++) {
            String packageName = a.get(i).topActivity.getPackageName();
            String className = a.get(i).topActivity.getClassName();
            if (className.equals("com.google.android.apps.youtube.tv.activity.TvPlayerActivity")) {
                try {
                    Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
                    method.invoke(am, packageName);  //packageName是需要强制停止的应用程序包名
                    Log.e(TAG, "packageName: " + packageName + "   className:" + className);
                    break;
                    //am.killBackgroundProcesses(packageName);
                } catch (Exception e) {
                    Log.e(TAG, e.toString());
                }
//                forceStopPackage.setAccessible(true);
//                forceStopPackage.invoke(am, "包名");
            }

        }

    }
}
